{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "87cf55f2-1c4e-4e83-a20d-eaaeca9b9aea",
   "metadata": {},
   "source": [
    "# Pandas处理重复值与异常值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "da68f81e-16a0-49cf-85c0-df735e4e8cec",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd568ab9-625f-40d8-8f9d-45e37dfcbe62",
   "metadata": {},
   "source": [
    "## 1、删除重复行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2c7e2658-c929-4032-a350-9d3342a53e7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_df(indexs,columns):\n",
    "    data=[[str(j)+str(i) for j in columns]for i in indexs]\n",
    "    df = pd.DataFrame(data=data,index=indexs,columns=columns)\n",
    "    return df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e46819b-00a0-4cbe-8f43-cc5e42368786",
   "metadata": {},
   "source": [
    "- 使用duplicated()函数检测重复行\n",
    "- - 放回元素为布尔类型的Series对象\n",
    "  - 每个元素对应一行，如果改行不是第一次出现，则元素为True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d1756006-0d4a-4d68-8c23-83f2f44b27ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A1</td>\n",
       "      <td>B1</td>\n",
       "      <td>C1</td>\n",
       "      <td>D1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A4</td>\n",
       "      <td>B4</td>\n",
       "      <td>C4</td>\n",
       "      <td>D4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "1  A1  B1  C1  D1\n",
       "2  A2  B2  C2  D2\n",
       "3  A3  B3  C3  D3\n",
       "4  A4  B4  C4  D4"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = make_df([1,2,3,4],list('ABCD'))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f8297387-e482-431b-8c58-edf62619012e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A4</td>\n",
       "      <td>B4</td>\n",
       "      <td>C4</td>\n",
       "      <td>D4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "1  A2  B2  C2  D2\n",
       "2  A2  B2  C2  D2\n",
       "3  A3  B3  C3  D3\n",
       "4  A4  B4  C4  D4"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 让第一行和第二行重复\n",
    "df.loc[1]=df.loc[2]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ca99a12a-b1a0-42f0-b9d5-5d1a0b025372",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1     True\n",
       "2     True\n",
       "3    False\n",
       "4    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断是否和前面的行重复了\n",
    "df.duplicated()\n",
    "\n",
    "# 默认first\n",
    "df.duplicated(keep='first') # 保留第一行（默认第一行不重复）\n",
    "\n",
    "df.duplicated(keep='last')# 保留最后一行\n",
    "\n",
    "df.duplicated(keep=False)# 不保留重复的行，标记所有重复的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bff48d0d-4c47-4726-b474-dc06f1f12667",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>DDD</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A4</td>\n",
       "      <td>B4</td>\n",
       "      <td>C4</td>\n",
       "      <td>D4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C    D\n",
       "1  A2  B2  C2  DDD\n",
       "2  A2  B2  C2   D2\n",
       "3  A3  B3  C3   D3\n",
       "4  A4  B4  C4   D4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[1,'D']='DDD'\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4aecccac-348c-4965-90be-9380eb69321b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    False\n",
       "2     True\n",
       "3    False\n",
       "4    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# subset:子集，默认判断所有数据\n",
    "df.duplicated(subset=['A','B','C'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ba7ba6b-de4e-405b-9b09-dab42e1a9fc8",
   "metadata": {},
   "source": [
    "- 使用drop_duplicates()函数删除重复的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9bf4814b-8c72-4b43-9ec8-4caab5c3c5c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A2</td>\n",
       "      <td>B2</td>\n",
       "      <td>C2</td>\n",
       "      <td>D2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A3</td>\n",
       "      <td>B3</td>\n",
       "      <td>C3</td>\n",
       "      <td>D3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A4</td>\n",
       "      <td>B4</td>\n",
       "      <td>C4</td>\n",
       "      <td>D4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D\n",
       "2  A2  B2  C2  D2\n",
       "3  A3  B3  C3  D3\n",
       "4  A4  B4  C4  D4"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.drop_duplicates()\n",
    "df.drop_duplicates(subset=['A','B','C'])\n",
    "df.drop_duplicates(subset=['A','B','C'],keep='last')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e44531f-4477-4d09-b766-7ee659a484f6",
   "metadata": {},
   "source": [
    "## 2.映射"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35e03854-18ea-4795-9bfa-9ffaf8d38fe3",
   "metadata": {},
   "source": [
    "映射的含义：创建一个映射相关关系表，把values元素和一个特定的标签或者字符串绑定。\n",
    "包含三种操作：\n",
    "- replace()函数：替换元素\n",
    "- map()函数：新建一列\n",
    "- rename()函数：替换索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc059cd4-bd64-49cc-ba64-ba08a91cc7af",
   "metadata": {},
   "source": [
    "**1)replace()函数：替换函数**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3f77568-412e-40c4-972d-ee24aadef09b",
   "metadata": {},
   "source": [
    "使用repalce()函数，对values进行替换操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e0308b9a-0e32-4e93-98b3-ee420641ec05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>H5</th>\n",
       "      <th>UI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>24</td>\n",
       "      <td>67</td>\n",
       "      <td>6</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>73</td>\n",
       "      <td>93</td>\n",
       "      <td>38</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>97</td>\n",
       "      <td>86</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>24</td>\n",
       "      <td>66</td>\n",
       "      <td>71</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Python  Java  H5  UI\n",
       "张三      24    67   6  25\n",
       "李四      73    93  38  63\n",
       "王五      66    97  86   3\n",
       "赵六      24    66  71  21"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index=['张三','李四','王五','赵六']\n",
    "columns=['Python','Java','H5','UI']\n",
    "data=np.random.randint(0,100,size=(4,4))\n",
    "df=pd.DataFrame(data=data,index=index,columns=columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3568c63e-28be-4bfa-bbc3-2a36ddc8a249",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>H5</th>\n",
       "      <th>UI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>24</td>\n",
       "      <td>67</td>\n",
       "      <td>6</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>73</td>\n",
       "      <td>93</td>\n",
       "      <td>38</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>97</td>\n",
       "      <td>86</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>24</td>\n",
       "      <td>66</td>\n",
       "      <td>71</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Python  Java  H5   UI\n",
       "张三      24    67   6   25\n",
       "李四      73    93  38   63\n",
       "王五      66    97  86    3\n",
       "赵六      24    66  71  100"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将指定值的元素替换\n",
    "df.replace({20:50,21:100})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34ef878d-865a-4512-89c6-64d9f65765e0",
   "metadata": {},
   "source": [
    "**2）map()函数：适合处理某一单独的列**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a729f86-e69a-48bd-bf2b-0b6bad2a17a5",
   "metadata": {},
   "source": [
    "map()函数中可以使用lambda函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "dc1c7808-014f-4141-b129-a68aba5f68d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>H5</th>\n",
       "      <th>UI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>24</td>\n",
       "      <td>67</td>\n",
       "      <td>6</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>73</td>\n",
       "      <td>93</td>\n",
       "      <td>38</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>97</td>\n",
       "      <td>86</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>24</td>\n",
       "      <td>66</td>\n",
       "      <td>71</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Python  Java  H5  UI\n",
       "张三      24    67   6  25\n",
       "李四      73    93  38  63\n",
       "王五      66    97  86   3\n",
       "赵六      24    66  71  21"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.copy()\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a5a900e1-89e3-47c0-b123-97ac4368c003",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>H5</th>\n",
       "      <th>UI</th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Java是否及格</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>24</td>\n",
       "      <td>67</td>\n",
       "      <td>6</td>\n",
       "      <td>25</td>\n",
       "      <td>240</td>\n",
       "      <td>及格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>73</td>\n",
       "      <td>93</td>\n",
       "      <td>38</td>\n",
       "      <td>63</td>\n",
       "      <td>730</td>\n",
       "      <td>及格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>97</td>\n",
       "      <td>86</td>\n",
       "      <td>3</td>\n",
       "      <td>660</td>\n",
       "      <td>及格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>24</td>\n",
       "      <td>66</td>\n",
       "      <td>71</td>\n",
       "      <td>21</td>\n",
       "      <td>240</td>\n",
       "      <td>及格</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Python  Java  H5  UI  Pandas Java是否及格\n",
       "张三      24    67   6  25     240       及格\n",
       "李四      73    93  38  63     730       及格\n",
       "王五      66    97  86   3     660       及格\n",
       "赵六      24    66  71  21     240       及格"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# map一般用在Series数据结构，不能用于DataFrame\n",
    "df2['Python'].map({24:100,73:200,66:300,24:400})\n",
    "\n",
    "# 将一列数据的值乘以十\n",
    "df2['Python'].map(lambda x:x*10)#lambda x:x*10 类似于Scala函数简写 (x=>x*10)\n",
    "\n",
    "# 新增一列\n",
    "df2['Pandas']=df2['Python'].map(lambda x:x*10)\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6ee9182c-57c6-446d-817f-2723e4368df8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>H5</th>\n",
       "      <th>UI</th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Java是否及格</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>24</td>\n",
       "      <td>67</td>\n",
       "      <td>6</td>\n",
       "      <td>25</td>\n",
       "      <td>240</td>\n",
       "      <td>及格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>73</td>\n",
       "      <td>93</td>\n",
       "      <td>38</td>\n",
       "      <td>63</td>\n",
       "      <td>730</td>\n",
       "      <td>及格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>97</td>\n",
       "      <td>86</td>\n",
       "      <td>3</td>\n",
       "      <td>660</td>\n",
       "      <td>及格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>24</td>\n",
       "      <td>66</td>\n",
       "      <td>71</td>\n",
       "      <td>21</td>\n",
       "      <td>240</td>\n",
       "      <td>及格</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Python  Java  H5  UI  Pandas Java是否及格\n",
       "张三      24    67   6  25     240       及格\n",
       "李四      73    93  38  63     730       及格\n",
       "王五      66    97  86   3     660       及格\n",
       "赵六      24    66  71  21     240       及格"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 新增一列：判断Java的成绩是否及格\n",
    "df2['Java是否及格'] = df2['Java'].map(lambda n:'及格' if n >= 60 else '不及格')\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "0640fe45-76b8-4e78-bf48-148c7f142181",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>H5</th>\n",
       "      <th>UI</th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Java是否及格</th>\n",
       "      <th>UI等级</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>24</td>\n",
       "      <td>67</td>\n",
       "      <td>6</td>\n",
       "      <td>25</td>\n",
       "      <td>240</td>\n",
       "      <td>及格</td>\n",
       "      <td>不及格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>73</td>\n",
       "      <td>93</td>\n",
       "      <td>38</td>\n",
       "      <td>63</td>\n",
       "      <td>730</td>\n",
       "      <td>及格</td>\n",
       "      <td>及格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>97</td>\n",
       "      <td>86</td>\n",
       "      <td>3</td>\n",
       "      <td>660</td>\n",
       "      <td>及格</td>\n",
       "      <td>不及格</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>24</td>\n",
       "      <td>66</td>\n",
       "      <td>71</td>\n",
       "      <td>21</td>\n",
       "      <td>240</td>\n",
       "      <td>及格</td>\n",
       "      <td>不及格</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Python  Java  H5  UI  Pandas Java是否及格 UI等级\n",
       "张三      24    67   6  25     240       及格  不及格\n",
       "李四      73    93  38  63     730       及格   及格\n",
       "王五      66    97  86   3     660       及格  不及格\n",
       "赵六      24    66  71  21     240       及格  不及格"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用普通函数\n",
    "# 新增一列:判断UI成绩\n",
    "# <60 不及格\n",
    "# 60<=n<80 及格\n",
    "# >=80 优秀\n",
    "\n",
    "def fn(n):\n",
    "    if n <60:\n",
    "        return '不及格'\n",
    "\n",
    "    elif n<80:\n",
    "        return '及格'\n",
    "\n",
    "    return '优秀'\n",
    "df2['UI等级']=df2['UI'].map(fn)\n",
    "df2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86709213-3a5e-4ddf-96e9-bd31a3f20d21",
   "metadata": {},
   "source": [
    "**3)rename()函数：替换索引**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "4bbd4ee3-4042-43b2-8eb4-f149f6e458e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>H5</th>\n",
       "      <th>UI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>24</td>\n",
       "      <td>67</td>\n",
       "      <td>6</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>73</td>\n",
       "      <td>93</td>\n",
       "      <td>38</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>97</td>\n",
       "      <td>86</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>24</td>\n",
       "      <td>66</td>\n",
       "      <td>71</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Python  Java  H5  UI\n",
       "张三      24    67   6  25\n",
       "李四      73    93  38  63\n",
       "王五      66    97  86   3\n",
       "赵六      24    66  71  21"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df3 = df.copy()\n",
    "df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "0ce1584e-0c28-4644-afd5-7221377bd73d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>派森</th>\n",
       "      <th>Java</th>\n",
       "      <th>H5</th>\n",
       "      <th>UI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>24</td>\n",
       "      <td>67</td>\n",
       "      <td>6</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>73</td>\n",
       "      <td>93</td>\n",
       "      <td>38</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>97</td>\n",
       "      <td>86</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>赵六</th>\n",
       "      <td>24</td>\n",
       "      <td>66</td>\n",
       "      <td>71</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    派森  Java  H5  UI\n",
       "张三  24    67   6  25\n",
       "李四  73    93  38  63\n",
       "王五  66    97  86   3\n",
       "赵六  24    66  71  21"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# rename 默认修改行索引名\n",
    "df3.rename({'张三':'ZhangSan'})\n",
    "df3.rename(index={'张三':'ZhangSan'})\n",
    "\n",
    "# 修改列索引名\n",
    "df3.rename({'Python':'派森'},axis=1)\n",
    "df3.rename(columns={'Python':'派森'})\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "5be4d0bf-ad3f-40a3-96e4-2620825ec4ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>H5</th>\n",
       "      <th>UI</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>张三</td>\n",
       "      <td>24</td>\n",
       "      <td>67</td>\n",
       "      <td>6</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>李四</td>\n",
       "      <td>73</td>\n",
       "      <td>93</td>\n",
       "      <td>38</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>王五</td>\n",
       "      <td>66</td>\n",
       "      <td>97</td>\n",
       "      <td>86</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>赵六</td>\n",
       "      <td>24</td>\n",
       "      <td>66</td>\n",
       "      <td>71</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  index  Python  Java  H5  UI\n",
       "0    张三      24    67   6  25\n",
       "1    李四      73    93  38  63\n",
       "2    王五      66    97  86   3\n",
       "3    赵六      24    66  71  21"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重置索引\n",
    "df3.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "17529a52-6013-482b-9521-4d5fc4a777f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>UI</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H5</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>24</td>\n",
       "      <td>67</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>73</td>\n",
       "      <td>93</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>86</th>\n",
       "      <td>66</td>\n",
       "      <td>97</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71</th>\n",
       "      <td>24</td>\n",
       "      <td>66</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Python  Java  UI\n",
       "H5                  \n",
       "6       24    67  25\n",
       "38      73    93  63\n",
       "86      66    97   3\n",
       "71      24    66  21"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定某一列设置为行索引\n",
    "df3.set_index(keys=['H5'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4321dd48-9a6b-4781-a5de-64e3ed030b79",
   "metadata": {},
   "source": [
    "**4）apply()函数：即支持Series,也支持DataFrame**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "c173559f-6953-43bb-9be7-8648c2ce3159",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>9</td>\n",
       "      <td>9</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "A       5      9       2\n",
       "B       8      0       8\n",
       "C       9      9       5\n",
       "D       0      5       5\n",
       "E       5      1       2"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(data=np.random.randint(0,10,size=(5,3)),\n",
    "                 index=list('ABCDE'),\n",
    "                 columns=['Python','NumPy','Pandas'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "bbb2a1af-8739-4541-800f-3052eb335bb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    False\n",
       "B     True\n",
       "C     True\n",
       "D    False\n",
       "E    False\n",
       "Name: Python, dtype: bool"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用于Series，其中lambad相当于Scala中的作为形参的函数\n",
    "# x表示这个列的每一个数据\n",
    "df['Python'].apply(lambda x: True if x>5 else False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "60877382-7d41-41a0-9c06-c7045554d19f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    5.333333\n",
       "B    5.333333\n",
       "C    7.666667\n",
       "D    3.333333\n",
       "E    2.666667\n",
       "dtype: float64"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用于DataFrame\n",
    "# x表示DataFrame中的某列或某行的Series数据\n",
    "\n",
    "# 默认求每列的平均值\n",
    "df.apply(lambda x:x.mean())\n",
    "# 求行的平均值\n",
    "df.apply(lambda x:x.mean(),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "0b26bd21-b3f3-4cd0-b2de-0080d85be20d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    (5.3, 3)\n",
       "B    (5.3, 3)\n",
       "C    (7.7, 3)\n",
       "D    (3.3, 3)\n",
       "E    (2.7, 3)\n",
       "dtype: object"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 自定义方法\n",
    "def fn2(x):\n",
    "    return (np.round(x.mean(),1),x.count())\n",
    "\n",
    "df.apply(fn2)\n",
    "df.apply(fn2,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "c6249d10-cbb6-42e6-bdbe-befaf3ed1092",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\21232\\AppData\\Local\\Temp\\ipykernel_33240\\1586893823.py:2: FutureWarning: DataFrame.applymap has been deprecated. Use DataFrame.map instead.\n",
      "  df.applymap(lambda x:x+100)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>105</td>\n",
       "      <td>109</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>108</td>\n",
       "      <td>100</td>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>109</td>\n",
       "      <td>109</td>\n",
       "      <td>105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>100</td>\n",
       "      <td>105</td>\n",
       "      <td>105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>105</td>\n",
       "      <td>101</td>\n",
       "      <td>102</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "A     105    109     102\n",
       "B     108    100     108\n",
       "C     109    109     105\n",
       "D     100    105     105\n",
       "E     105    101     102"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# applymap:DataFrame专有的方法,其中的x是每个元素\n",
    "df.applymap(lambda x:x+100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0bb503e-c38a-46c5-b652-d41de50dc8fd",
   "metadata": {},
   "source": [
    "**5)transform()函数**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "6613f860-59ba-4312-99f2-b15d62a3b9f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "A       1      6       1\n",
       "B       3      6       5\n",
       "C       4      0       6\n",
       "D       6      4       0\n",
       "E       5      7       4"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(data=np.random.randint(0,10,size=(5,3)),\n",
    "                 index=list('ABCDE'),\n",
    "                 columns=['Python','NumPy','Pandas'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "039d8551-cff7-4213-9ffb-988f57055725",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>-10</td>\n",
       "      <td>-60</td>\n",
       "      <td>-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>30</td>\n",
       "      <td>60</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-40</td>\n",
       "      <td>0</td>\n",
       "      <td>-60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>-60</td>\n",
       "      <td>-40</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>50</td>\n",
       "      <td>70</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "A     -10    -60     -10\n",
       "B      30     60      50\n",
       "C     -40      0     -60\n",
       "D     -60    -40       0\n",
       "E      50     70      40"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series中使用transform\n",
    "# 可以执行多项计算\n",
    "df['Python'].transform([np.sqrt,np.exp])\n",
    "\n",
    "# DataFrame中使用transfrom\n",
    "def convert(x):\n",
    "    if x.mean() >4:\n",
    "        return x * 10\n",
    "    return x * (-10)\n",
    "\n",
    "# 默认列\n",
    "df.transform(convert) # 处理每一列\n",
    "df.transform(convert,axis=1)# 处理每一行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e80d13b9-6bd0-497f-a8e5-d71e12f0d0da",
   "metadata": {},
   "source": [
    "## 3.Pandas异常值处理和过滤"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd3daea4-4eac-4639-ad72-8ab50b9e53e5",
   "metadata": {},
   "source": [
    "- describe():查看每一列的描述性统计量\n",
    "- df.info():查看数据信息\n",
    "- df.std():可以求得DataFrame对象每一列的标准差\n",
    "- df.drop():删除特定索引\n",
    "- unique():唯一,去重\n",
    "- query():按条件查询\n",
    "- df.sort_values():根据值排序\n",
    "- df.sort_index():根据索引排序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df23544e-984c-48e2-b767-ec54f0397f52",
   "metadata": {},
   "source": [
    "- describe():查看每一列的描述性统计量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "2786fc1a-7b4d-4bce-8c9a-005f768c9b61",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "A       1      6       1\n",
       "B       3      6       5\n",
       "C       4      0       6\n",
       "D       6      4       0\n",
       "E       5      7       4"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "95d82381-1913-4ef3-bab2-5a4d503a4686",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>1%</th>\n",
       "      <th>30%</th>\n",
       "      <th>40%</th>\n",
       "      <th>50%</th>\n",
       "      <th>90%</th>\n",
       "      <th>99%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Python</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.8</td>\n",
       "      <td>1.923538</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.08</td>\n",
       "      <td>3.2</td>\n",
       "      <td>3.6</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.6</td>\n",
       "      <td>5.96</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NumPy</th>\n",
       "      <td>5.0</td>\n",
       "      <td>4.6</td>\n",
       "      <td>2.792848</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.16</td>\n",
       "      <td>4.4</td>\n",
       "      <td>5.2</td>\n",
       "      <td>6.0</td>\n",
       "      <td>6.6</td>\n",
       "      <td>6.96</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pandas</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.2</td>\n",
       "      <td>2.588436</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.04</td>\n",
       "      <td>1.6</td>\n",
       "      <td>2.8</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5.6</td>\n",
       "      <td>5.96</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        count  mean       std  min    1%  30%  40%  50%  90%   99%  max\n",
       "Python    5.0   3.8  1.923538  1.0  1.08  3.2  3.6  4.0  5.6  5.96  6.0\n",
       "NumPy     5.0   4.6  2.792848  0.0  0.16  4.4  5.2  6.0  6.6  6.96  7.0\n",
       "Pandas    5.0   3.2  2.588436  0.0  0.04  1.6  2.8  4.0  5.6  5.96  6.0"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()\n",
    "df.describe([0.01,0.3,0.4,0.9,0.99])\n",
    "df.describe([0.01,0.3,0.4,0.9,0.99]).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d926bbf0-1a53-40a3-bce1-3333bd5057a2",
   "metadata": {},
   "source": [
    "- df.std():可以求得DataFrame对象每一列的标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "e125ef6d-0c85-4b00-bfb2-fd360a2cdc44",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Python    1.923538\n",
       "NumPy     2.792848\n",
       "Pandas    2.588436\n",
       "dtype: float64"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.std()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3c9d637-df70-4699-847e-ea67f9ee59a7",
   "metadata": {},
   "source": [
    "- df.drop():删除特定索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "7c052974-9cf9-4f3f-aef9-b863800a84ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "A       1      6       1\n",
       "B       3      6       5\n",
       "C       4      0       6\n",
       "D       6      4       0\n",
       "E       5      7       4"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.copy()\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "36e51a5c-350c-4148-83f1-e0ea6da15a7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "C       4      0       6\n",
       "D       6      4       0\n",
       "E       5      7       4"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 默认删除行\n",
    "df2.drop('A')# 删除行\n",
    "df2.drop('Python',axis=1)# 删除列\n",
    "df2.drop(index='A')# 删除行\n",
    "df2.drop(columns='Python')# 删除列\n",
    "\n",
    "# 删除多列多行\n",
    "df2.drop(columns=['NumPy','Python'])\n",
    "df2.drop(index=['A','B'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d305805b-c158-4496-9d10-a0b80d01d84e",
   "metadata": {},
   "source": [
    "- unique():唯一去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "f662f538-6133-4b52-8c9f-e59ff32b958e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "A       1      6       1\n",
       "B       3      6       5\n",
       "C       4      0       6\n",
       "D       6      4       0\n",
       "E       5      7       4"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "8141fdb7-1b72-4364-b70f-2ee64c34eb57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 0, 4, 7], dtype=int32)"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# DataFrame没有unique属性\n",
    "# 需要使用Series去调用\n",
    "df['NumPy'].unique()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57301461-e968-4054-8cb8-7a01369312ad",
   "metadata": {},
   "source": [
    "- df.query:按条件查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "f6184c92-9d97-4ecb-b3bf-0d30af718112",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "A       1      6       1\n",
       "B       3      6       5\n",
       "C       4      0       6\n",
       "D       6      4       0\n",
       "E       5      7       4"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "c3e1ef1b-8dba-47c0-8359-e15581ef9420",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "C       4      0       6"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ==,>,<\n",
    "# and(&),or(|)\n",
    "# in\n",
    "df.query('Python==4')# 找到Python中等于4的所有行\n",
    "df.query('Python>4')\n",
    "df.query('Python<4')\n",
    "\n",
    "df.query('Python>4 and Python<6')\n",
    "df.query('Python>4 & Python<6')\n",
    "\n",
    "df.query('Python in[3,4,5,6]')# 成员运算符\n",
    "\n",
    "# 使用变量\n",
    "n=4\n",
    "df.query('Python == @n')# @n 表示使用变量n的值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9125d368-801a-4c83-83b2-c8330ee1f61b",
   "metadata": {},
   "source": [
    "- df.sort_values():根据值排序\n",
    "- df.sort_index():根据索引排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "cbd3c454-6be7-4828-8f06-f41c502d1fae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "A       1      6       1\n",
       "B       3      6       5\n",
       "C       4      0       6\n",
       "D       6      4       0\n",
       "E       5      7       4"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "4e1d1d79-4683-47ef-a979-96b437789ba5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Python</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>7</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   NumPy  Python  Pandas\n",
       "A      6       1       1\n",
       "B      6       3       5\n",
       "C      0       4       6\n",
       "D      4       6       0\n",
       "E      7       5       4"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 默认列名和升序排序，对应的整个行排序\n",
    "df.sort_values('NumPy')\n",
    "# 默认为True(升序)\n",
    "df.sort_values('Python',ascending=False)#降序排序\n",
    "\n",
    "\n",
    "# 使用行索引排序，对应的整个列排序\n",
    "df.sort_values('C',axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "4e720192-95b5-49c2-8c42-628659a8f650",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Pandas</th>\n",
       "      <th>NumPy</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Pandas  NumPy\n",
       "A       1       1      6\n",
       "B       3       5      6\n",
       "C       4       6      0\n",
       "D       6       0      4\n",
       "E       5       4      7"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照索引名排序\n",
    "df.sort_index()\n",
    "# 默认对行索引进行升序排序\n",
    "df.sort_index(ascending=False)\n",
    "\n",
    "# 列名进行排序\n",
    "df.sort_index(ascending=False,axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fabd8025-f607-4a27-9dd8-b70fc822cecc",
   "metadata": {},
   "source": [
    "- df.info():查看数据信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "ed147ad9-26dd-47df-82da-b1b56fb7c7e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 5 entries, A to E\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype\n",
      "---  ------  --------------  -----\n",
      " 0   Python  5 non-null      int32\n",
      " 1   NumPy   5 non-null      int32\n",
      " 2   Pandas  5 non-null      int32\n",
      "dtypes: int32(3)\n",
      "memory usage: 272.0+ bytes\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80072802-a225-49d9-9d88-ba728afb8d82",
   "metadata": {},
   "source": [
    "## 4.抽样"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abc1cc18-e7e4-41f4-b787-d62ad3fb967a",
   "metadata": {},
   "source": [
    "- 使用.take()函数排序\n",
    "- 可以借助np.random.permutation()函数随机排序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfdfc5dd-1e58-4696-b645-037031706390",
   "metadata": {},
   "source": [
    "无返回抽样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "e89e6ca9-9353-4cac-b399-0873ba07df6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "A       1      6       1\n",
       "B       3      6       5\n",
       "C       4      0       6\n",
       "D       6      4       0\n",
       "E       5      7       4"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "id": "a427fdd1-c46e-46b3-9c53-318e4289e523",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>5</td>\n",
       "      <td>7</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "B       3      6       5\n",
       "C       4      0       6\n",
       "D       6      4       0\n",
       "E       5      7       4\n",
       "A       1      6       1"
      ]
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.take([1,0,2]) # 行排列\n",
    "df2.take([1,0,2],axis=1) # 列排列\n",
    "\n",
    "# 随机排列\n",
    "np.random.permutation([0,1,2])\n",
    "\n",
    "# 无放回抽：样依次随机取出，没有重复值\n",
    "df2.take(np.random.permutation([0,1,2,3,4]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c6092e2-d3b9-40e4-95f7-069d5e48b875",
   "metadata": {},
   "source": [
    "有放回抽样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "id": "a4a934bc-f0e6-4c8f-ad95-015d14a1e088",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>6</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  NumPy  Pandas\n",
       "C       4      0       6\n",
       "B       3      6       5\n",
       "D       6      4       0\n",
       "D       6      4       0\n",
       "A       1      6       1"
      ]
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 有放回抽样：可能会出现重复值\n",
    "np.random.randint(0,5,size=5)\n",
    "df2.take(np.random.randint(0,5,size=5))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
